package com.jy.wms.controller;


import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsReceivingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsReceivingController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author JaneLiu
 * @Date Tue Sep 26 17:30:08 CST 2017
 */
@Controller
@RequestMapping("/wmsreceiving")
@Api(value = "/wmsreceiving", description = "关于收货操作")
public class WmsReceivingController extends BaseController<WmsReceivingService,WmsReceiving> {

    @PostMapping(value = "/receiving")
    @ApiOperation(value = "码盘确认收货 确认收货",response = JSONObject.class, notes = "POST确认收货")
    public @ResponseBody JSONObject addReceiving(@RequestBody List<WmsPalletSorting> sorting, HttpSession session) {
        try {
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            WmsShelfRule rule=baseService.seachSjgz(hz.get(0).getHzid());
            baseService.addReceiving(userinfo.getUser().getOrgId().get(0).toString(),hz.get(0).getHzid(),sorting);

            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"确认收货成功","","");
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"确认收货失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"确认收货失败",e.getMessage(),"");
        }
    }
    @PostMapping(value = "/againReceiving")
    @ApiOperation(value = "码盘再次收货 再次收货",response = JSONObject.class, notes = "POST再次收货")
    public @ResponseBody JSONObject againReceiving(@RequestBody List<WmsPalletSorting> sorting, HttpSession session) {
        try {
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            WmsShelfRule rule=baseService.seachSjgz(hz.get(0).getHzid());
            baseService.addReceiving(userinfo.getUser().getOrgId().get(0).toString(), hz.get(0).getHzid(), sorting);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"码盘再次收货成功","",  JSONObject.fromObject(sorting).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"码盘再次收货失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"码盘再次收货失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/rollBackReceiving")
    @ApiOperation(value = "码盘收货 取消收货",response = JSONObject.class, notes = "POST取消收货")
    public @ResponseBody JSONObject rollBackReceiving(@RequestBody Map<String,Object> map) {
        try {
            List<String> ids=null;
            String qxry="";
            String qxyy="";
            if(map.containsKey("ids")){ids=JSONArray.fromObject(map.get("ids"));}
            if(map.containsKey("qxry")){qxry=map.get("qxry").toString();}
            if(map.containsKey("qxyy")){qxyy=map.get("qxyy").toString();}
            String asnBh= baseService.rollBackReceiving(ids,qxry,qxyy);
            if("".equals(asnBh)||asnBh==null){
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"取消收货成功","",  JSONObject.fromObject(map).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"取消收货成功","",  JSONObject.fromObject(map).toString());
            }

        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"取消收货失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"取消收货失败",e.getMessage(),"");
        }
    }
    @PostMapping(value = "/ASNreceiving/{id}")
    @ApiOperation(value = "ASN确认收货 确认收货",response = JSONObject.class, notes = "POST ASN确认收货")
    public @ResponseBody JSONObject addASNReceiving(@RequestBody List<WmsEstimatedArrivalOrderDetailed> detaileds,@PathVariable String id, HttpSession session) {
        try {
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            WmsShelfRule rule=baseService.seachSjgz(hz.get(0).getHzid());
            baseService.addASNReceiving(userinfo.getUser().getOrgId().get(0).toString(), hz.get(0).getHzid(), detaileds, id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "ASN确认收货成功", "", "");

        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"ASN确认收货失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"ASN确认收货失败",e.getMessage(),"");
        }
    }
    @PostMapping(value = "/againASNreceiving/{id}")
    @ApiOperation(value = "ASN再次收货 再次收货",response = JSONObject.class, notes = "POSTASN再次收货")
    public @ResponseBody JSONObject againASNreceiving(@RequestBody List<WmsEstimatedArrivalOrderDetailed> detaileds,@PathVariable String id,HttpSession session) {
        try {
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            WmsShelfRule rule=baseService.seachSjgz(hz.get(0).getHzid());
            baseService.addASNReceiving(userinfo.getUser().getOrgId().get(0).toString(), hz.get(0).getHzid(), detaileds,id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "ASN再次收货成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"ASN再次收货失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"ASN再次收货失败",e.getMessage(),"");
        }
    }
    @PostMapping(value = "/rollBackASNReceiving")
    @ApiOperation(value = "ASN收货 取消收货",response = JSONObject.class, notes = "POSTASN取消收货")
    public @ResponseBody JSONObject rollBackASNReceiving(@RequestBody Map<String,Object> map) {
        try {
            List<String> ids=null;
            String qxry="";
            String qxyy="";
            if(map.containsKey("ids")){ids= JSONArray.fromObject(map.get("ids"));}
            if(map.containsKey("qxry")){qxry=map.get("qxry").toString();}
            if(map.containsKey("qxyy")){qxyy=map.get("qxyy").toString();}
            String asnbh=baseService.rollBackASNReceiving(ids,qxry,qxyy);
            if("".equals(asnbh)||asnbh==null){
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"ASN取消收货成功","",  JSONObject.fromObject(map).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"ASN取消收货成功,"+asnbh+"失败","",  JSONObject.fromObject(map).toString());
            }

        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"ASN取消收货失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"ASN取消收货失败",e.getMessage(),"");
        }
    }
}
